﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Core;
using Windows.System;


using NaverOpenApiLibrary.Services;
using NaverOpenApiLibrary.Models;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace NaverOpenApiLibraryPortableForWindowsRT
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        // ****************************************************
        // REPLACE BELOW WITH YOUR API KEY
        // API Keys can be issued at: http://dev.naver.com/openapi/
        private const string SEARCH_API_KEY = "4f60b03829def6e96147d436882f10ec";
        // ****************************************************

        public MainPage()
        {
            this.InitializeComponent();
        }

        

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var bs = new BlogService(SEARCH_API_KEY);
            bs.ApiServiceCompleted += (s, blog) =>
                {
                    Debug.WriteLine("search done");
                    var blogItems = new List<BlogItem>(blog.Channel.Items);

                    foreach (BlogItem item in blogItems)
                    {
                        item.Title = Regex.Replace(item.Title, "<.*?>", string.Empty);
                    }

                    var ignore = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                    {
                        SearchResultListView.ItemsSource = blogItems;
                    });
                };
            bs.GetBlog("NHN", 1, 50, BlogService.Sort.Sim);

            var ns = new NewsSearchService(SEARCH_API_KEY);
            ns.ApiServiceCompleted += (s, news) =>
                {
                    var newsItems = new List<NewsDetail>(news.Channel.Item);

                    foreach (NewsDetail item in newsItems)
                    {
                        item.Title = Regex.Replace(item.Title, "<.*?>", string.Empty);
                    }

                    var ignore = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                        {
                            NewsSearchResultListView.ItemsSource = newsItems;
                        });
                };
            ns.GetNews("지스타", 1, 30);
        }

        private void SearchResultListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var senderListView = sender as ListView;
            var item = senderListView.SelectedItem as BlogItem;

            var ignore = Launcher.LaunchUriAsync(new Uri(item.Link, UriKind.Absolute));
        }

        private void NewsSearchResultListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var senderListView = sender as ListView;
            var item = senderListView.SelectedItem as NewsDetail;

            var ignore = Launcher.LaunchUriAsync(new Uri(item.Link, UriKind.Absolute));
        }
        
        
    }
}
